package com.line.tools.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.MessageDigest;

/**
 * 采用SHA-1加密工具类
 * 
 * @author Justin Yang
 * @version 1.0
 * @since 2016-07-12
 *
 */
public class SHA1Utils {
	public static String sha1SecretKey = "0123456789abcdefghijklmnopqrstuvwxyz";
	private static Logger log = LoggerFactory.getLogger(MD5Utils.class);

	/**
	 * 根据密钥进行SHA-1加密
	 * 
	 * @param s
	 *            需要加密的字符串
	 * @return 加密后的字符串
	 */
	public final static String SHA1Encrypt(String s) {
		char hexDigits[] = sha1SecretKey.toCharArray();
		try {
			byte[] btInput = s.getBytes();
			// 获得MD5摘要算法的 MessageDigest 对象
			MessageDigest mdInst = MessageDigest.getInstance("SHA-1");
			// 使用指定的字节更新摘要
			mdInst.update(btInput);
			// 获得密文
			byte[] md = mdInst.digest();
			// 把密文转换成十六进制的字符串形式
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			log.info("SHA-1 encrypt has exception:", e);
			return null;
		}
	}

	public static void main(String[] args) {

	}
}
